package com.sgb.member.view_model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.MenuBean;
import com.sgb.member.entity.PermissionHuiYuanBean;
import com.sgb.member.service.MemberAPI;
import com.sgb.member.ui.fragment.HomeMemberFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.AppFolwLogApi;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.entity.EnterpriseEditEntity;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by WYJ on 2020/8/26.
 */
public class HomeMemberModel extends BaseViewModel {

    private final HomeMemberFragment mFragment;
    private final MemberRepertory repertory;
    private final MemberAPI memberAPI;
    private final AppFolwLogApi memberAppAPI;

    public HomeMemberModel(HomeMemberFragment homeMemberFragment, MemberRepertory repertory) {
        mFragment = homeMemberFragment;
        this.repertory = repertory;
        memberAPI = MemberConfig.getInstance().getApi();
        memberAppAPI = MemberConfig.getInstance().AppFolwLogApi();
    }

    public void getMenuList() {
        memberAPI.getMenuIcons2("huiyuan").enqueue(new ICallBack<BaseEntity<MenuBean>>() {
            @Override
            public void onSuccess(BaseEntity<MenuBean> data) {
                if (data != null) {
                    mFragment.refreshMenus2(data.getData().getAppIconList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MenuBean>> call, Throwable t) {
                super.onFailure(call, t);
                mFragment.closeRecyclerRefreshing();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getAccountMemberSize(int comType,String no) {
        if (!DialogHelper.isShowing()) {
            DialogHelper.showProgressDialog(mFragment.getContext(), "数据加载中...");
        }
        memberAPI.judgeHasMember(comType,no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if (data != null) {
                    boolean isPerson = MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_PERSONAL;
                    mFragment.initViewPager(isPerson, data.getData() > 0);
                } else {
                    mFragment.closeRecyclerRefreshing();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Integer>> call, Throwable t) {
                super.onFailure(call, t);
                mFragment.closeRecyclerRefreshing();
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                super.onFailureWithEntity(entity);
                boolean isPerson = MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_PERSONAL;
                mFragment.initViewPager(isPerson, false);
            }

            @Override
            public void onFinally() {

            }
        });
    }

    int i = 0;
    /**
     * 根据用户身份信息获取展示的企业标题
     */
    public void getCompanyTitleList(String areaCode) {
        i ++;
        areaCode = repertory.getAreaCode();
        SharedPreferenceUtil.getInstance().saveString(SharedPreferenceUtil.AREA_CODE, repertory.getAreaCode());
        int code = TextUtils.isEmpty(areaCode)?615000:Integer.valueOf(areaCode);
        memberAppAPI.getEnterpriseRecListNew(code).enqueue(new ICallBack<BaseEntity<List<EnterpriseEditEntity>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseEditEntity>> data) {
                i = 0;
                mFragment.initCompanyTitle(data.getData());
            }

            @Override
            public void onFailureWithEntity(BaseEntity entity) {
                super.onFailureSwitchErrCode(entity);
                if (entity != null && i< 2){
                    if ("53999".equals(entity.getErrCode())){
                        getCompanyTitleList("");
                    }
                }
            }
        });
    }

    /*
     *更新会员权限
     */
    public void getPermissionHuiYuan() {
        //返回页面数据
        memberAPI.getPermissionHuiYuan().enqueue(new Callback<BaseEntity<List<PermissionHuiYuanBean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PermissionHuiYuanBean>>> call, Response<BaseEntity<List<PermissionHuiYuanBean>>> response) {
                if (response != null && response.body() != null && response.body().getData() != null) {
                    String s = new Gson().toJson(response.body().getData(), new TypeToken<List<PermissionHuiYuanBean>>() {}.getType());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERMISSION_HUI_YUAN, s);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PermissionHuiYuanBean>>> call, Throwable t) {
            }
        });
    }

}
